﻿
//Author: 张亚涛
//createtime:2015-12-15 23:08:47
//Description:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using NS.DAL.Systems;
using Model;
using DAL;

namespace DAL
{
    /// <summary>
    /// 该类提供了一系列操作 "Member" 表的方法;
    /// </summary>
    public partial class MemberBase
    {
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 根据Member表的OpenID字段返回数据 
        /// </summary>
        /// <param name="OpenID">OpenID</param>
        /// <returns></returns>       
        public static Member GetMemberByOpenID(string OpenID)
        {
            Member member = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [OpenID] = @OpenID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@OpenID",OpenID)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                member = new Member(reader);
            }
            reader.Close();
            return member;
        }
        /// <summary>
        /// 判断微信用户是否已注册
        /// </summary>
        /// <param name="OpenID">OpenID</param>
        /// <returns>True/Flase</returns>
        public static bool IsRegister(string OpenID)
        {
            string SQL_Select = @"select count(*) from Member where OpenID=@OpenID";
            SqlParameter[] parameters ={
                                         new SqlParameter("@OpenID",OpenID)
                                       };
            string ResultStr = SQLHelper.ExecuteScalar(SQL_Select, parameters);
            if (!string.IsNullOrEmpty(ResultStr) && int.Parse(ResultStr) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 判断手机号是否已注册
        /// </summary>
        /// <param name="Mobile">手机号</param>
        /// <returns>True表示存在，Flase表示不存在</returns>
        public static bool IsExistMobile(string Mobile)
        {
            string SQL_Select = @"select count(*) from Member where Mobile=@Mobile";
            SqlParameter[] parameters ={
                                         new SqlParameter("@Mobile",Mobile)
                                       };
            string ResultStr = SQLHelper.ExecuteScalar(SQL_Select, parameters);
            if (!string.IsNullOrEmpty(ResultStr) && int.Parse(ResultStr) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 根据手机号获取会员ID
        /// </summary>
        /// <param name="Mobile">手机号</param>
        /// <returns></returns>
        public static string GetOpenIDbyMobile(string Mobile)
        {
            string SQL_Select = @"select OpenID from Member where Mobile=@Mobile";
            SqlParameter[] parameters ={
                                         new SqlParameter("@Mobile",Mobile)
                                       };
            string ResultStr = SQLHelper.ExecuteScalar(SQL_Select, parameters);
            return ResultStr;
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 返回Member表的所有数据 （SQL语句）
        /// </summary>
        /// <param name="Mobile">手机号</param> 
        /// <returns></returns>        
        public static string GetAllMember_SQL(string Mobile)
        {
            string strSQL = "SELECT m.* FROM Member m WHERE 1=1 ";
            if (!string.IsNullOrEmpty(Mobile))
            {
                strSQL += " AND m.Mobile='" + Mobile + "' ";
            }
            return strSQL;
        }
        #endregion
    }
}